var pages = {
    'number_keys': ['date', 'regdate', 'lastlogin', 'pages', 'ntopics'],
    'exec': function (str, dialog) {
        dialog = (dialog) ? true : false;
        var ss = str.split(/^(forum|topic|user)\/(view|list)(\/(\d+))?/);
        var page = ss[1];
        var mode = ss[2];
        var id = ss[4];
        var pars = ss[5].split('/');
        var params = {};
        for (var i = 0; i < pars.length; i++)
        {
            var p = pars[i].split("=");
            params[p[0]] = p[1];
        }
        if (!dialog) {
            $('#content').html(loadstr);
        }
        this[page][mode](id, params);
    },
    'childrenObj': function (obj) {
        if (!obj || !obj.text()) {
            return false;
        }
        var result = {};
        var c = $(obj).children();
        c.each(function () {
            var n = $(this)[0].tagName;
            if($(this).text().match(/^(\s)+$/g)) { // Check if it's whitespace
                result[n] = "";
            }
            else if ($(this).children().length > 0) {
                result[n] = $(this);
            } else {
                if (in_array(n, pages.number_keys)) {
                    result[n] = Number($(this).text());
                } else {
                    result[n] = $(this).text();
                }
            }
        });
        return result;
    },
    'contacts': function (contact, id) {
        var html = '';
        html += '<td><img alt="Private Message" title="Private Message" src="images/icons/contact/pm.png" /></td>';
        if (contact.email) {
            html += '<td><a href="mailto:' + contact.email + '">';
            html += '<img alt="EMail" title="EMail" src="images/icons/contact/email.png" /></a></td>';
        } else {
            html += "<td />";
        }
        if (contact.aim) {
            html += '<td><a href="aim:goim?screenname=' + contact.aim + '"><img alt="AIM" title="AIM" src="images/icons/contact/aim.png" /></a></td>';
        } else {
            html += "<td />";
        }
        if (contact.msn) {
            html += "<td><a onclick=\"showcont('MSN', '";
            html += contact.msn + "', '" + id + "')\">";
            html += '<img alt="Google Talk" title="MSN" src="images/icons/contact/msn.png" /></a></td>';
        } else {
            html += "<td />";
        }
        if (contact.skype) {
            html += '<td><a href="skype:' + contact.skype + '?chat"><img alt="Skype" title="Skype" src="images/icons/contact/skype.png" /></a></td>';
        } else {
            html += "<td />";
        }
        if (contact.gtalk) {
            html += "<td><a onclick=\"showcont('GTalk', '";
            html += contact.gtalk + "', '" + id + "')\">";
            html += '<img alt="Google Talk" title="Google Talk" src="images/icons/contact/gtalk.png" /></a></td>';
        } else {
            html += "<td />";
        }
        return html;
    }
};
pages.forum = {
    'view': function (id, params) {
        var page = Number((params.page) ? params.page : 1);
        var pp = Number((params.pp) ? params.pp : 15);
        $.get('api.xml?r=forum&fid=' + id + '&page=' + page + '&pp=' + pp, function (data) {
            var forum = pages.childrenObj($(data).children('forum'));
            var cookies = cookie();
            var sl = Number((conf.forum.sidelinks > 0) ? conf.forum.sidelinks : 2);
            var html = '';
            // Top of page
            html += '<script>pp = ' + pp + ';</script>';
            html += '<h1 class="list">' + forum.name + '</h1>';
            html += '<div id="fnav">';
            html += '<a href="forum/list">Forums</a> > <a href="forum/view/' + id + '">' + forum.name + '</a>';
            html += '</div>';
            // Topic list
            html += '<table class="list" id="topics">';
            html += '<thead>';
            html += '<tr>';
            html += '<th>Topic Name<br /><small>Topic Description</small></th>';
            html += '<th>User</th>';
            html += '<th class="count">Replies</th>';
            html += '<th>Last Post</th>';
            html += '</tr>';
            html += '</thead>';
            html += '<tfoot>';
            html += '<tr>';
            html += '<td>';
            if (cookies.id) {
                html += '<a href="topic.php?m=new;' + forum.id;
                html += '&amp;height=400&amp;width=500&amp;buttons=ntopic" class="dialog">';
                html += i18n.get('nav/forums/ntopic') + '</a>';
            }
            html += '</td>';
            html += '<td colspan="3" id="pages">';
            html += '<form action="forum/view/' + forum.id + '/page=' + page + '/pp=' + pp;
            html += '&amp;order=asc" onsubmit="fp(' + forum.id + ',$(\'#jump\').val(),' + sl + '); return false;" id="pjump">';
            html += '<div>Jump to page <input type="text" value="' + page + '" name="page" id="jump" /> of ' + forum.pages + '</div>';
            html += '</form>';
            html += '</td>';
            html += '</tr>';
            html += '</tfoot>';
            // Topics
            html += '<tbody>';
            if (forum.topics) {
                forum.topics.children('topic').each(function () {
                    var topic = pages.childrenObj($(this));
                    var tid = $(this).children('id').text();
                    var ttitle = $(this).children('title').text();
                    var tdesc = $(this).children('desc').text();
                    var tuid = topic.user.children('id').text();
                    var tuname = topic.user.children('name').text();
                    var tnposts = $(this).children('nposts').text() || 0;
                    var tlp = pages.childrenObj(topic.lp);
                    var last = '';
                    html += '<tr>';
                    html += '<td><a href="topic/view/' + topic.id + '">' + topic.title + '</a>';
                    if (topic.desc) {
                        html += '<br /><small>' + topic.desc + '</small>';
                    }
                    html += '</td>';
                    html += '<td><a href="user/view/' + tuid;
                    html += '/dims=350x450" class="dialog">' + tuname + '</a></td>';
                    html += '<td class="count">' + tnposts + '</td>';
                    if (!tlp) {
                        last = 'No new posts.';
                    } else {
                        var luid = tlp.user.children('id').text();
                        var luname = tlp.user.children('name').text();
                        last = date("F j, Y g:i:s A", tlp.date);
                        last += '<br />by <a href="user/view/' + luid + '/dims=350x450" class="dialog">' + luname + '</a>.';
                    }
                    html += '<td>' + last + '</td>';
                    html += '</tr>';
                });
            } else {
                html += '<tr><td colspan="4">No topics to display.</td></tr>';
            }
            html += '</tbody>';
            $("#content").html(html);
            $('#pages').sidelinks("f", forum.id, page, sl, forum.pages);
            pload();
        });
    },
    'list': function (id, params) {
        $.get('api.xml?r=forums', function (data) {
            var xml = $(data).children('forums');
            var forums = xml.children('forum');
            var html = '';
            html += '<table class="list" id="forums">';
            html += '<thead>';
            html += '<tr>';
            html += '<th>Forum Name<br /><small>Forum Description</small></th>';
            html += '<th class="count">Topics</th>';
            html += '<th>Last Post</th>';
            html += '</tr>';
            html += '</thead>';
            html += '<tfoot>';
            html += '<tr>';
            html += '<td colspan="3" id="pages">Page 1 of 1</td>';
            html += '</tr>';
            html += '</tfoot>';
            html += '<tbody>';
            forums.each(function () {
                var lp = pages.childrenObj($(this).children('topics').children('topic').children('lp'));
                var ntopics = $(this).children('ntopics').text();
                var fid = $(this).children('id').text();
                var fname = $(this).children('name').text();
                var fdesc = $(this).children('desc').text();
                var last = '';
                html += '<tr>';
                html += '<td><a href="forum/view/' + fid + '">' + fname + '</a>';
                if (fdesc) {
                    html += '<br /><small>' + fdesc + '</small>';
                }
                html += '</td>';
                html += '<td class="count">' + ntopics + '</td>';
                if (!lp) {
                    last = 'No new posts';
                } else {
                    var ldate = lp.date;
                    var luid = lp.user.children('id').text();
                    var luname = lp.user.children('name').text();
                    last = date("F j, Y g:i:s A", ldate);
                    last += '<br />by <a href="user/view/' + luid + '/dims=350x450" class="dialog">' + luname + '</a>.';
                }
                html += '<td>' + last + '</td>';
                html += '</tr>';
            });
            html += '</tbody>';
            html += '</table>';
            $("#content").html(html);
            pload();
        });
    }
};
pages.topic = {
    'view': function (id, params) {
        var page = (params.page) ? params.page : 1;
        var pp = (params.pp) ? params.pp : 15;
        $.get('api.xml?r=topic&tid=' + id + '&page=' + page + '&pp=' + pp, function (data) {
            var topic = pages.childrenObj($(data).children('topic'));
            var cookies = cookie();
            var sl = Number((conf.topic.sidelinks > 0) ? conf.topic.sidelinks : 2);
            var posts = (topic.posts) ? topic.posts.children('post') : null;
            var user = pages.childrenObj(topic.user);
            var contacts = pages.childrenObj(user.contact);
            var SID = cookies.PHPSESSID;
            var tedit = (cookies.id === user.id || cookies.rank >= 2);
            var html = '';
            // Top links
            html += '<div id="fnav">';
            html += '<a href="forum/list">Forums</a> > <a href="forum/view/' + topic.fid + '">' + topic.fname + '</a> > <a href="topic/view/' + id + '">' + topic.title + '</a>';
            html += '</div>';
            // Start topic box
            html += '<div class="topic">';
            html += '<div class="cont" id="t' + id + '">';
            // Topic Userbar
            html += '<div class="userbar">';
            html += '<h1><a href="user/view/' + user.id + '/dims=350x450" class="dialog">' + user.name + '</a></h1>';
            html += '<h3>' + user.title + '</h3>';
            html += '<div class="avatar">';
            if (user.avatar) {
                html += "\n\t\t\t\t<img src=\"uploads/avatars/" + user.avatar + "\" />\n\t\t\t";
            }
            html += '</div>';
            html += '<table class="contact">';
            html += '<tr>';
            html += pages.contacts(contacts, "t" + id);
            html += '</tr>';
            html += '</table>';
            html += '<span class="contact_hidden" id="ct' + id + '"></span>';
            html += '<span>Topics: ' + user.topics.length + '</span><br />';
            html += '<span>Posts: ' + user.posts + '</span>';
            html += '</div>';
            // Topic content
            html += '<table class="body_cont">';
            html += '<tr class="info">';
            html += '<th>' + topic.title + '</th>';
            if (tedit) {
                html += '<th>' + date("F j, Y g:i:s A", topic.date) + '</th>';
                html += '<th><a onclick="edit(' + id + ', \'t\');">[E]</a></th>';
            } else {
                html += '<th colspan="2">' + date("F j, Y g:i:s A", topic.date) + '</th>';
            }
            html += '</tr><tr>';
            html += '<td colspan="3" class="body">';
            html += '<div class="tbody">' + topic.body + '</div>';
            html += '<div class="traw">';
            html += '<form action="topic.php?m=edit;' + id + '&amp;PHPSESSID=' + SID + '" method="post" ';
            html += 'onsubmit="$(\'.traw form\').ajaxSubmit({beforeSubmit: function (){$(\'#t' + id + ' .subs\').html(loadstr);}';
            html += ', success: function (){edit(' + id + ', \'t\',1);}, target: \'#t' + id + ' .tbody\'}); return false;">';
            html += '<div><textarea rows="5" name="body" cols="20">' + topic.raw + '</textarea></div>';
            html += '</form>';
            html += '</div></td></tr>';
            if (user.signature) {
                html += '<tr>';
                html += '<td colspan="3" class="sig">' + user.signature + '</td>';
                html += '</tr>';
            }
            html += '</table>';
            html += '</div>';
            // Posts
            if (posts) {
                html += '<div id="posts">';
                posts.each(function () {
                    var post = pages.childrenObj($(this));
                    var user = pages.childrenObj($(this).children('user'));
                    var topics = user.topics.length;
                    var pedit = (cookies.id === user.id || cookies.rank >= 2);
                    var contacts = pages.childrenObj(user.contact);
                    html += '<div class="cont" id="p' + post.id + '">';
                    html += '<div class="userbar">';
                    html += '<h1><a href="user/view/' + user.id + '/dims=350x450" class="dialog">' + user.name + '</a></h1>';
                    html += '<h3>' + user.title + '</h3>';
                    html += '<div class="avatar">';
                    if (user.avatar) {
                        html += "\n\t\t\t\t<img src=\"uploads/avatars/" + user.avatar + "\" />\n\t\t\t";
                    }
                    html += '</div>';
                    html += '<table class="contact">';
                    html += '<tr>';
                    html += pages.contacts(contacts, 'p' + post.id);
                    html += '</tr>';
                    html += '</table>';
                    html += '<span class="contact_hidden" id="cp' + post.id + '"></span>';
                    html += '<span>Topics: ' + topics + '</span><br />';
                    html += '<span>Posts: ' + user.posts + '</span>';
                    html += '</div>';
                    html += '<table class="body_cont">';
                    html += '<tr class="info">';
                    html += '<th>' + post.title + '</th>';
                    if (pedit) {
                        html += '<th>' + date("F j, Y g:i:s A", post.date) + '</th>';
                        html += '<th><a onclick="edit(' + post.id + ', \'p\');">[E]</a></th>';
                    } else {
                        html += '<th colspan="2">' + date("F j, Y g:i:s A", post.date) + '</th>';
                    }
                    html += '</tr><tr>';
                    html += '<td colspan="3" class="body">';
                    html += '<div class="pbody">' + post.body + '</div>';
                    html += '<div class="praw">';
                    html += '<form action="post.php?m=edit;' + post.id + '&amp;PHPSESSID=' + SID + '" method="post" ';
                    html += 'onsubmit="$(this).ajaxSubmit({beforeSubmit: function (){$(\'#p' + post.id + ' .subs\').html(loadstr);}, target: \'#p' + post.id + ' .pbody\'';
                    html += ', success: function (){edit(' + post.id + ', \'p\',1);}}); return false;">';
                    html += '<div><textarea rows="5" name="body" cols="20">' + post.raw + '</textarea></div>';
                    html += '</form>';
                    html += '</div></td></tr>';
                    if (user.signature) {
                        html += '<tr>';
                        html += '<td colspan="3" class="sig">' + user.signature + '</td>';
                        html += '</tr>';
                    }
                    html += '</table>';
                    html += '</div>';
                });
                html += '</div>';
            }
            // Reply box
            if (cookies.id) {
                html += '<div id="reply">';
                html += '<form action="post.php?m=new;' + id + '&amp;PHPSESSID=' + SID + '" id="replyf" onsubmit="$(\'#replyf\').ajaxSubmit({ success: function (d) { checkreply(d,' + id + '); } }); return false;" method="post">';
                html += '<table>';
                html += '<tr>';
                html += '<td><textarea name="body" rows="4" cols="20"></textarea></td>';
                html += '</tr><tr>';
                html += '<td><input type="submit" class="button" value="Post Reply" /></td>';
                html += '</tr></table></form>';
                html += '<div id="replyres"></div>';
                html += '</div>';
            }
            // Pagelinks
            html += '<div id="tpages">';
            html += '<form action="topic/view/' + id + '/page=' + page + '/pp=' + pp + '/order=asc" onsubmit="tp(' + id + ',$(\'#jump\').val(),' + sl + '); return false;" id="pjump">';
            html += '<div>Jump to page <input type="text" value="' + page + '" name="page" id="jump" /> of ' + topic.pages + '</div>';
            html += '</form>';
            html += '</div>';
            html += '</div>';
            $("#content").html(html);
            $('#tpages').sidelinks("t", id, page, sl, topic.pages);
            pload();
        });
    }
};
pages.user = {
    'view': function (id, params) {
        if (params.dims) {
            var dims = params.dims.split("x"); // height x width
            $("#dialog").dialog("option", "height", Number(dims[0]));
            $("#dialog").dialog("option", "width", Number(dims[1]));
        }
        if (params.width) {
            $("#dialog").dialog("option", "width", Number(params.width));
        }
        if (params.height) {
            $("#dialog").dialog("option", "height", Number(params.height));
        }
        $("#dialog").dialog("option", "modal", true);
        $("#dialog").dialog("option", "position", "center");
        var modal = (!params.modal) ? true : false;
        $("#dialog").dialog("option", "closeOnEscape", modal);
        $("#dialog").html(loadstr);
        $.get('api.xml?r=user&id=' + id, function (data) {
            var user = pages.childrenObj($(data).children('user'));
            var contacts = pages.childrenObj(user.contact);
            var html = '';
            html += '<div id="viewprof">';
            html += '<h1>' + user.name + '</h1>';
            if (user.title) {
                html += '<h3>' + user.title + '</h3>';
            }
            html += '<div class="avatar">';
            if (user.avatar) {
                html += '<img src=\"uploads/avatars/' + user.avatar + '\" />';
            }
            html += '</div>';
            html += '<table class="contact">';
            html += '<tr>';
            html += pages.contacts(contacts, 0);
            html += '</tr>';
            html += '</table>';
            html += '<span class="contact_hidden" id="c0"></span>';
            html += '<h2>Info</h2>';
            html += '<table id="info" class="uinfo">';
            html += '<tr>';
            html += '<td>Topics:&nbsp;' + user.topics.length + '</td>';
            html += '<td>Joined:&nbsp;' + date("F j, Y g:i:s A", user.regdate) + '</td>';
            html += '</tr>';
            html += '<tr>';
            html += '<td>Posts:&nbsp;' + user.posts + '</td>';
            html += '<td>Last Login:&nbsp;' + date("F j, Y g:i:s A", user.lastlogin) + '</td>';
            html += '</tr>';
            html += '</table>';
            if (user.topics) {
                html += '<h2>Recent Topics</h2>';
                html += '<table id="rectop" class="uinfo">';
                var i = 1;
                user.topics.children('topic').slice(-3).each(function () {
                    var topic = pages.childrenObj($(this));
                    html += '<tr><td>';
                    html += '<a href="topic/view/' + topic.id + '">' + topic.title + '</a> ';
                    html += '<a onclick="tprev(' + i + ');" id="tpa' + i + '">v</a>';
                    html += '<div id="tprev' + i + '" class="tprev">';
                    html += trunc(strip_tags(topic.body, "<b><i><u><br>"), 50);
                    html += '</div></td></tr>';
                    i++;
                });
                html += '</table>';
            }
            html += '</div>';
            $("#dialog").dialog("option", "title", user.name);
            if (!$("#dialog").dialog("isOpen")) {
                $("#dialog").dialog("open");
            }
            $("#dialog").html(html);
            pload();
        });
    }
};
$(function () {
    $("#dialog").dialog({ modal: true, autoOpen: false }); // Initialize dialog
});